﻿namespace BEGDC.CC.Web.UserControls.ESCCV
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using System.Collections;

	using BEGDC.CC.BL.CCFlow.ESCCV;
	using BEGDC.CC.BL.CCFlow.CR;
	using BEGDC.CC.Entity;
	using BEGDC.CC.BL.Workflow;
	using BEGDC.CC.BL.Common;
	using BEGDC.CC.BL.Common.Query;
	using BEGDC.Utility.Castle;

	/// <summary>
	///		Summary description for Close.
	/// </summary>
	public class Close : BaseESCCV
	{
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.Button btnSubmit;
		protected System.Web.UI.WebControls.Button btnQuery;
		protected System.Web.UI.WebControls.Button btnSave;
		protected System.Web.UI.WebControls.TextBox tbOperationFlag;
		protected System.Web.UI.WebControls.TextBox tbOperationContent;
		protected System.Web.UI.WebControls.TextBox tbComment;
		protected ESCCVCtrl uclESCCV;
		protected BEGDC.CC.Web.UserControls.CV.CVPrintCtrl cvPrintCtrl;

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if(!Page.IsPostBack)
			{
				Session[SessionFlag.CostCenter] = null;

				this.btnSubmit.Attributes["onclick"] = "javascript:OnSubmit();";
				this.btnQuery.Attributes.Add("onclick", "javascript:OpenQueryPane("+Request.QueryString["AppID"]+");");
				
				this.uclESCCV.SetCloseControlVisiable();
				this.uclESCCV.BindCloseControl();
				this.uclESCCV.SetCloseControlEnable();
			}
			ButtonStatusSetting();
			uclESCCV.isReadOnly = true;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnQuery.Click += new EventHandler(this.btnQuery_Click);
			this.btnSubmit.Click += new EventHandler(this.btnSubmit_Click);
			this.btnSave.Click += new EventHandler(btnSave_Click);
			//			this.dgCostCenter.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgCostCenter_DeleteCommand);
			//			this.dgCostCenter.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgCostCenter_ItemDataBound);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnSubmit_Click(object sender, EventArgs e)
		{
			try
			{
				this.uclESCCV.SaveCloseControl();
				IESCCV ocrService = AppContainer.Instance[ServiceConst.ESCCVService] as IESCCV;
				ocrService.Close(Convert.ToInt64(Request.QueryString["FlowID"]), ESCCVTransitionList.Close, WebUtility.DeCodeString(Server, tbComment.Text));
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');</script><script>window.close();</script>");
			}
			catch
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
			}
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			try
			{
				//				SaveApplication();
				this.uclESCCV.SaveCloseControl();
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');</script>");
			}
			catch
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
			}
		}

		private void btnQuery_Click(object sender, EventArgs e)
		{
			try
			{
				if(this.tbOperationFlag.Text == "query")
				{
					IESCCV ocvService = AppContainer.Instance[ServiceConst.ESCCVService] as IESCCV;
					ocvService.Query(Convert.ToInt64(Request.QueryString["FlowID"]), this.tbOperationContent.Text, Session["Comment"].ToString());
				}
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');</script><script>window.close();</script>");
			}
			catch
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
			}
		}

		private void ControlStatusSetting(Accessibility access )
		{
			if( access == Accessibility.Readonly )
			{
				this.DisableChildrenControls(this);
			}
		}


		private void Btn_Export_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				ExcelTool.ICCExcelExport excelExport=new ExcelTool.ExportCVForm();
				excelExport.Export(this.Page, this.AppID );

				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');</script>");
			}
			catch (BEGDC.Utility.ExceptionManagement.GDCException exp)
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.ErrorMessage.Export_0001 + "');</script>");
			}
		}

		protected override void ButtonStatusSetting()
		{			
			if( this.Access == null )
				return;

			this.btnQuery.Visible = this.Access.IsButtonVisible(ESCCVTransitionList.Query);
			this.btnSubmit.Visible = this.Access.IsButtonVisible(ESCCVTransitionList.Close);
            this.uclESCCV.cvPrintCtrl.ProcessType = WorkflowTypes.ESCCV;
            this.uclESCCV.cvPrintCtrl.Visible = this.Access.IsButtonVisible(ESCCVTransitionList.Copy);
		}
	}
}
